﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SD.IBUS;
using SD.Entities;
using SD.IDAL;
using SD.DAL;
using System.Data;
using System.Data.SqlClient;

namespace SD.BUS
{
    public class DirectorateBIZ : BaseBIZ, IDirectorateBIZ
    {
        #region IDirectorateBIZ Members

        IDirectorateDAO directorateDAO= new DirectorateDAO();
        /// <summary>
        /// Get all Directorate add into list Directorate
        /// </summary>
        /// <param name="filterExpression"></param>
        /// <param name="includeInactive"></param>
        /// <returns>list Directorate</returns>
        public DataTable GetAll(string filterExpression, bool includeInactive, string orgId)
        {
            DataTable dtbResult = new DataTable();
            //Create DataColumn
            dtbResult.Columns.Add("DirectorateID");
            dtbResult.Columns.Add("Name");
            dtbResult.Columns.Add("Line1");
            dtbResult.Columns.Add("Postcode");
            dtbResult.Columns.Add("Surname");
            dtbResult.Columns.Add("IsActive");

            try
            {
                List<Directorate> lstDrect = directorateDAO.GetAll(filterExpression, includeInactive,orgId);
                if (lstDrect.Count > 0)
                {
                    foreach (Directorate drect in lstDrect)
                    {
                        dtbResult.Rows.Add(drect.DirectorateId, drect.Name,
                                            drect.Address != null ? drect.Address.Line1 : "N/A",
                                            drect.Address != null ? drect.Address.PostCode : "N/A",
                                            drect.Contact != null ? drect.Contact.FirstName : "N/A",
                                            drect.IsActive);
                    }
                }
            }
            catch (SqlException ex)
            {
                throw ex;
            }

            return dtbResult;
        }
        /// <summary>
        /// Get Data from Directorate Table with status and orgId
        /// </summary>
        /// <param name="includeInactive"></param>
        /// <param name="orgId"></param>
        /// <returns></returns>
        public DataTable GetAllFilter(bool includeInactive, string orgId)
        {
            DataTable dtbResult = new DataTable();
            dtbResult.Columns.Add("DirectorateID");
            dtbResult.Columns.Add("Name");
            dtbResult.Columns.Add("Line1");
            dtbResult.Columns.Add("Postcode");
            dtbResult.Columns.Add("Surname");
            dtbResult.Columns.Add("IsActive");

            try
            {
                List<Directorate> lstDrect = directorateDAO.GetAllFilter(includeInactive,orgId);
                if (lstDrect.Count > 0)
                {
                    foreach (Directorate drect in lstDrect)
                    {
                        dtbResult.Rows.Add(drect.DirectorateId, drect.Name,
                                            drect.Address != null ? drect.Address.Line1 : "N/A",
                                            drect.Address != null ? drect.Address.PostCode : "N/A",
                                            drect.Contact != null ? drect.Contact.FirstName : "N/A",
                                            drect.IsActive);
                    }
                }
            }
            catch (SqlException ex)
            {
                throw ex;
            }

            return dtbResult;
        }
        /// <summary>
        /// Insert a Directorate
        /// </summary>
        /// <param name="directorate"></param>
        public void Insert(Directorate directorate)
        {
            try
            {
                // insert organization
                directorate.DirectorateId = directorateDAO.Insert(directorate);

            }
            catch (SqlException ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// Get Directorate by Name
        /// </summary>
        /// <param name="dirName"></param>
        /// <returns></returns>
        public List<Directorate> GetDirByName(string dirName)
        {
            List<Directorate> result = new List<Directorate>();

            try
            {
                result = directorateDAO.GetDirByName(dirName);
            }
            catch (SqlException ex)
            {
                throw ex;
            }

            return result;
        }
        /// <summary>
        /// Get Directorate by Id
        /// </summary>
        /// <param name="dirId"></param>
        /// <returns></returns>
        public Directorate GetById(string dirId)
        {
            Directorate result = null;

            try
            {
                result = directorateDAO.GetById(dirId);
            }
            catch (SqlException ex)
            {
                throw ex;
            }

            return result;
        }
        /// <summary>
        /// Update a Directorate
        /// </summary>
        /// <param name="dir"></param>
        public void Update(Directorate dir)
        {
            try
            {
                directorateDAO.Update(dir);
            }
            catch (SqlException ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// Set active status of an Directorate
        /// </summary>
        /// <param name="dirId">ID of the Directorate</param>
        /// <param name="status">New active status of the Directorate</param>
        public void SetActive(string dirId, bool status)
        {
            try
            {
                directorateDAO.SetActive(dirId, status);
            }
            catch (SqlException ex)
            {
                throw ex;
            }

        }
        #endregion
    }
}
